<?php

class FilmController extends Lib2k_Controller_Action{


    public function preDispatch(){
        
   	}

    public function searchAction(){
        if ($this->getRequest()->isPost()) 
        {
            $params['searchtext'] = $this->getRequest()->getParam('searchtext'); 
            $params['cat'] = $this->getRequest()->getParam('cat');
            $redirectArray['controller'] = 'film';
            $redirectArray['action'] = 'search';
            if( $params['searchtext']){
                $redirectArray['searchtext'] = $params['searchtext'];    
            }
            
            $redirectArray['cat'] = $params['cat'];
            $this->_helper->Redirector->gotoRoute($redirectArray, 'default', true); 
           // $this->_helper->redirector('index', 'index', 'default', $params, true);
        }
        $filmModel = new Model_Film();
        $requestParams = $this->getRequest()->getParams();
        if(empty($requestParams['searchtext'])){
            $requestParams['searchtext'] = null;
        }
        if(empty($requestParams['cat'])){
            $requestParams['cat'] = null;
        }        
        
        $page = (int) $this->getRequest()->getParam('page', 1);
        $countPerPage = 10;
        
        $cats = $filmModel->getCategories(28);
        
        $films = $filmModel->search($requestParams['searchtext'], $requestParams['cat'], $countPerPage, ($page - 1) * $countPerPage );
        $filmsCount = $filmModel->searchCount($requestParams['searchtext'], $requestParams['cat']);
        $filmsCount = $filmsCount[0]['count'];
      //  Zend_Debug::dump($filmsCount);
        
        foreach($films as &$film) {
            $film['categories'] = $filmModel->getCategories($film['id']);
        }        
        
        $searchForm = new Form_FilmSearchForm();
        $searchForm->setAction("/film/search/");
        $searchForm->populate($requestParams);
        
        $searchForm->isValid($requestParams);
        

        $paginator = Zend_Paginator::factory($filmsCount);
        $paginator->setItemCountPerPage($countPerPage);
        $paginator->setCurrentPageNumber($page);
        
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('p/paginator.phtml');
        
        $this->view->paginator = $paginator;          
        
        $this->view->films = $films;
        $this->view->filmsCount = $filmsCount;
        $this->view->searchForm = $searchForm;
       // $this->_redirect("/index/index/param");
    }
    

    public function viewAction(){
    
     // $this->_helper->viewRenderer('search');  // the name of the action to render instead
     // $this->searchAction();  // call foo action now
      $this->_forward('search', 'film');
      return true;
      exit();
      
    
    }

}

